package com.student.servlet;

import com.student.dao.GradeDAO;
import com.student.model.Grade;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

@WebServlet("/grade")
public class GradeServlet extends HttpServlet {
    private GradeDAO gradeDAO = new GradeDAO();
    private SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String action = request.getParameter("action");
        if (action == null) {
            action = "list";
        }

        switch (action) {
            case "list":
                list(request, response);
                break;
            case "edit":
                showEditForm(request, response);
                break;
            case "delete":
                deleteGrade(request, response);
                break;
            default:
                list(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String action = request.getParameter("action");

        switch (action) {
            case "add":
                addGrade(request, response);
                break;
            case "update":
                updateGrade(request, response);
                break;
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        String studentNumber = request.getParameter("studentNumber");
        String courseName = request.getParameter("courseName");
        String semester = request.getParameter("semester");

        List<Grade> grades;
        if (studentNumber != null || courseName != null || semester != null) {
            grades = gradeDAO.search(studentNumber, courseName, semester);
        } else {
            grades = gradeDAO.findAll();
        }

        request.setAttribute("gradeList", grades);
        request.getRequestDispatcher("grade-list.jsp").forward(request, response);
    }

    private void showEditForm(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Grade grade = gradeDAO.findById(id);
        request.setAttribute("grade", grade);
        request.getRequestDispatcher("grade-edit.jsp").forward(request, response);
    }

    private void addGrade(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try {
            Grade grade = new Grade();
            grade.setStudentNumber(request.getParameter("studentNumber"));
            grade.setCourseName(request.getParameter("courseName"));
            grade.setScore(Double.parseDouble(request.getParameter("score")));
            grade.setSemester(request.getParameter("semester"));
            grade.setExamDate(dateFormat.parse(request.getParameter("examDate")));

            if (gradeDAO.add(grade)) {
                response.sendRedirect("grade?action=list");
            } else {
                request.setAttribute("error", "添加成绩失败");
                request.getRequestDispatcher("grade-add.jsp").forward(request, response);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            request.setAttribute("error", "日期格式错误");
            request.getRequestDispatcher("grade-add.jsp").forward(request, response);
        }
    }

    private void updateGrade(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        try {
            Grade grade = new Grade();
            grade.setId(Integer.parseInt(request.getParameter("id")));
            grade.setStudentNumber(request.getParameter("studentNumber"));
            grade.setCourseName(request.getParameter("courseName"));
            grade.setScore(Double.parseDouble(request.getParameter("score")));
            grade.setSemester(request.getParameter("semester"));
            grade.setExamDate(dateFormat.parse(request.getParameter("examDate")));

            if (gradeDAO.update(grade)) {
                response.sendRedirect("grade?action=list");
            } else {
                request.setAttribute("error", "更新成绩失败");
                request.setAttribute("grade", grade);
                request.getRequestDispatcher("grade-edit.jsp").forward(request, response);
            }
        } catch (ParseException e) {
            e.printStackTrace();
            request.setAttribute("error", "日期格式错误");
            request.getRequestDispatcher("grade-edit.jsp").forward(request, response);
        }
    }

    private void deleteGrade(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        gradeDAO.delete(id);
        response.sendRedirect("grade?action=list");
    }
} 